home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
graphic
/
jpegsrc4.zip
/
JCSAMPLE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-11-05
|
17KB
|
461 lines
/*
* jcsample.c
*
* Copyright (C) 1991, 1992, Thomas G. Lane.
* This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file.
*
* This file contains downsampling routines.
* These routines are invoked via the downsample and
* downsample_init/term methods.
*
* An excellent reference for image resampling is
* Digital Image Warping, George Wolberg, 1990.
* Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
*
* The downsampling algorithm used here is a simple average of the source
* pixels covered by the output pixel. The hi-falutin sampling literature
* refers to this as a "box filter". In general the characteristics of a box
* filter are not very good, but for the specific cases we normally use (1:1
* and 2:1 ratios) the box is equivalent to a "triangle filter" which is not
* nearly so bad. If you intend to use other sampling ratios, you'd be well
* advised to improve this code.
*
* A simple input-smoothing capability is provided. This is mainly intended
* for cleaning up color-dithered GIF input files (if you find it inadequate,
* we suggest using an external filtering program such as pnmconvol). When
* enabled, each input pixel P is replaced by a weighted sum of itself and its
* eight neighbors. P's weight is 1-8*SF and each neighbor's weight is SF,
* where SF = (smoothing_factor / 1024).
* Currently, smoothing is only supported for 2h2v sampling factors.
*/
#include "jinclude.h"
/*
* Initialize for downsampling a scan.
*/
METHODDEF void
downsample_init (compress_info_ptr cinfo)
{
/* no work for now */
}
/*
* Downsample pixel values of a single component.
* This version handles arbitrary integral sampling ratios, without smoothing.
* Note that this version is not actually used for customary sampling ratios.
*/
METHODDEF void
int_downsample (compress_info_ptr cinfo, int which_component,
long input_cols, int input_rows,
long output_cols, int output_rows,
JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
JSAMPARRAY output_data)
{
jpeg_component_info * compptr = cinfo->cur_comp_info[which_component];
int inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v;
long outcol, outcol_h; /* outcol_h == outcol*h_expand */
JSAMPROW inptr, outptr;
INT32 outvalue;
#ifdef DEBUG /* for debugging pipeline controller */
if (output_rows != compptr->v_samp_factor ||
input_rows != cinfo->max_v_samp_factor ||
(output_cols % compptr->h_samp_factor) != 0 ||
(input_cols % cinfo->max_h_samp_factor) != 0 ||
input_cols*compptr->h_samp_factor != output_cols*cinfo->max_h_samp_factor)
ERREXIT(cinfo->emethods, "Bogus downsample parameters");
#endif
h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor;
v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor;
numpix = h_expand * v_expand;
numpix2 = numpix/2;
inrow = 0;
for (outrow = 0; outrow < output_rows; outrow++) {
outptr = output_data[outrow];
for (outcol = 0, outcol_h = 0; outcol < output_cols;
outcol++, outcol_h += h_expand) {
outvalue = 0;
for (v = 0; v < v_expand; v++) {
inptr = input_data[inrow+v] + outcol_h;
for (h = 0; h < h_expand; h++) {
outvalue += (INT32) GETJSAMPLE(*inptr++);
}
}
*outptr++ = (JSAMPLE) ((outvalue + numpix2) / numpix);
}
inrow += v_expand;
}
}
/*
* Downsample pixel values of a single component.
* This version handles the common case of 2:1 horizontal and 1:1 vertical,
* without smoothing.
*/
METHODDEF void
h2v1_downsample (compress_info_ptr cinfo, int which_component,
long input_cols, int input_rows,
long output_cols, int output_rows,
JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
JSAMPARRAY output_data)
{
int outrow;
long outcol;
register JSAMPROW inptr, outptr;
#ifdef DEBUG /* for debugging pipeline controller */
jpeg_component_info * compptr = cinfo->cur_comp_info[which_component];
if (output_rows != compptr->v_samp_factor ||
input_rows != cinfo->max_v_samp_factor ||
(output_cols % compptr->h_samp_factor) != 0 ||
(input_cols % cinfo->max_h_samp_factor) != 0 ||
input_cols*compptr->h_samp_factor != output_cols*cinfo->max_h_samp_factor)
ERREXIT(cinfo->emethods, "Bogus downsample parameters");
#endif
for (outrow = 0; outrow < output_rows; outrow++) {
outptr = output_data[outrow];
inptr = input_data[outrow];
for (outcol = 0; outcol < output_cols; outcol++) {
*outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr) + GETJSAMPLE(inptr[1])
+ 1) >> 1);
inptr += 2;
}
}
}
/*
* Downsample pixel values of a single component.
* This version handles the standard case of 2:1 horizontal and 2:1 vertical,
* without smoothing.
*/
METHODDEF void
h2v2_downsample (compress_info_ptr cinfo, int which_component,
long input_cols, int input_rows,
long output_cols, int output_rows,
JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
JSAMPARRAY output_data)
{
int inrow, outrow;
long outcol;
register JSAMPROW inptr0, inptr1, outptr;
#ifdef DEBUG /* for debugging pipeline controller */
jpeg_component_info * compptr = cinfo->cur_comp_info[which_component];
if (output_rows != compptr->v_samp_factor ||
input_rows != cinfo->max_v_samp_factor ||
(output_cols % compptr->h_samp_factor) != 0 ||
(input_cols % cinfo->max_h_samp_factor) != 0 ||
input_cols*compptr->h_samp_factor != output_cols*cinfo->max_h_samp_factor)
ERREXIT(cinfo->emethods, "Bogus downsample parameters");
#endif
inrow = 0;
for (outrow = 0; outrow < output_rows; outrow++) {
outptr = output_data[outrow];
inptr0 = input_data[inrow];
inptr1 = input_data[inrow+1];
for (outcol = 0; outcol < output_cols; outcol++) {
*outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1])
+ 2) >> 2);
inptr0 += 2; inptr1 += 2;
}
inrow += 2;
}
}
/*
* Downsample pixel values of a single component.
* This version handles the special case of a full-size component,
* without smoothing.
*/
METHODDEF void
fullsize_downsample (compress_info_ptr cinfo, int which_component,
long input_cols, int input_rows,
long output_cols, int output_rows,
JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
JSAMPARRAY output_data)
{
#ifdef DEBUG /* for debugging pipeline controller */
if (input_cols != output_cols || input_rows != output_rows)
ERREXIT(cinfo->emethods, "Pipeline controller messed up");
#endif
jcopy_sample_rows(input_data, 0, output_data, 0, output_rows, output_cols);
}
#ifdef INPUT_SMOOTHING_SUPPORTED
/*
* Downsample pixel values of a single component.
* This version handles the standard case of 2:1 horizontal and 2:1 vertical,
* with smoothing.
*/
METHODDEF void
h2v2_smooth_downsample (compress_info_ptr cinfo, int which_component,
long input_cols, int input_rows,
long output_cols, int output_rows,
JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
JSAMPARRAY output_data)
{
int inrow, outrow;
long colctr;
register JSAMPROW inptr0, inptr1, above_ptr, below_ptr, outptr;
INT32 membersum, neighsum, memberscale, neighscale;
#ifdef DEBUG /* for debugging pipeline controller */
jpeg_component_info * compptr = cinfo->cur_comp_info[which_component];
if (output_rows != compptr->v_samp_factor ||
input_rows != cinfo->max_v_samp_factor ||
(output_cols % compptr->h_samp_factor) != 0 ||
(input_cols % cinfo->max_h_samp_factor) != 0 ||
input_cols*compptr->h_samp_factor != output_cols*cinfo->max_h_samp_factor)
ERREXIT(cinfo->emethods, "Bogus downsample parameters");
#endif
/* We don't bother to form the individual "smoothed" input pixel values;
* we can directly compute the output which i